package com.controller;

import com.Table.User;
import com.model.operation.UserOperate;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by LPFma on 2016/12/5 0005.
 */
@WebServlet(name = "CheckSelfInformation", urlPatterns = "/CheckSelfInformation")
public class CheckSelfInformation extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*init response*/
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.addHeader("Access-Control-Allow-Origin","*");
        response.addHeader("Access-Control-Allow-Credentials", "true");
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");

        String username = (String) request.getSession().getAttribute("username");
        Map map = new HashMap<String, String>();

        do {
            if (username == null) break;
            UserOperate userOperate = new UserOperate();
            User user = userOperate.CheckSelfInformation(username);

            /* pack json map */
            map.put("nickname", user.getNickname());
            map.put("sex", user.getSex());
            map.put("coinnum", user.getCoinnum().toString());
            map.put("signature", user.getSignature());
            map.put("signupdate", user.getSignupdate().toString());
        } while (false);

        /* send json */
        JSONObject jsonObject = JSONObject.fromObject(map);
        response.getWriter().write(jsonObject.toString());

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
